//===============================================================================
// Q4Tech Engineering Team
// Mobile Updater Application Block - November 2006
//===============================================================================
// Copyright  Q4Tech Informatica Argentina.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using Q4Tech.Engineering.Ipc;
using System;
using System.Collections.Generic;
using System.Text;
using Q4Tech.Engineering.UpdaterAgent.Common;

namespace Q4Tech.Engineering.UpdaterAgent
{
    internal class NotificationManager : IDisposable
    {
        private Dictionary<String, IpcCallClient<int, bool>> callClients = new Dictionary<string, IpcCallClient<int, bool>>();

        private IpcCallClient<int, bool> GetCallClient(String appName)
        {
            if (!callClients.ContainsKey(appName))
            {
                string updatesAvailableName = String.Format("UpdaterService_UpdatesAvailable_{0}", appName.ToUpper());

                IpcCallClient<int, bool> client = new IpcCallClient<int, bool>(PointToPointMessageQueueFactory.Instance, updatesAvailableName);

                callClients[appName] = client;
            }

            return callClients[appName];
        }

        public bool RaiseUpdateInProcess(String appName)
        {
            IpcCallClient<int, bool> updatesAvailableClient = GetCallClient(appName);

            try
            {
                if (updatesAvailableClient.ServerPresent)
                {
                    updatesAvailableClient.Timeout = 0;
                    return updatesAvailableClient.Exec((int)NotificationEventType.UpdateInProcess);
                }
            }
            catch (IpcCallTimeoutException)
            {
            }

            return true;
        }

        public bool RaiseUpdatesAvailable(String appName)
        {
            IpcCallClient<int, bool> updatesAvailableClient = GetCallClient(appName);

            try
            {
                if (updatesAvailableClient.ServerPresent)
                {
                    updatesAvailableClient.Timeout = 0;
                    return updatesAvailableClient.Exec((int)NotificationEventType.UpdateAvailable);
                }
            }
            catch (IpcCallTimeoutException)
            {    
            }

            return true;
        }

        #region IDisposable Members

        public void Dispose()
        {
            foreach (IpcCallClient<int, bool> client in callClients.Values)
                client.Dispose();
        }

        #endregion
    }
}
